package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    IUserInfoRedisService userInfoRedisService;

    @Reference
    ITravelService travelService;

    @Reference
    ITravelStatisVOService travelStatisVOService;

    @Reference
    IUserInfoService userInfoService;

    /**
     * spring容器启动，并且初始化之后，马上执行的操作
     * @param contextRefreshedEvent
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        System.out.println("---------------------------------------- vo数据初始化 start ------------------------------------------------");

        //攻略统计初始化
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            //如果vo已经存在了，跳过，防止再次初始化，覆盖数据
            if(strategyStatisVOService.isStrategyVOExist(detail.getId())){
                continue; //跳过就好
            }

            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(detail,vo);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());

            strategyStatisVOService.setStrategyStatisVO(vo);
        }


        //关注和粉丝 黑名单
        List<UserInfo> userInfos = userInfoService.list();
        List<Long> focusUserIdList = null;
        List<Long> fansUserIdList = null;
        List<Long> blackUserIdList = null;
        if(userInfos!=null && userInfos.size()>0){
            for (UserInfo userInfo : userInfos) {
                userInfoRedisService.setUserStatisVO(userInfo.getId());
                //关注
                focusUserIdList = userInfoService.getFocusUserIdList(userInfo.getId());
                if(focusUserIdList!=null&&focusUserIdList.size()>0){
                    for (Long focusUserId : focusUserIdList) {
                        if(userInfoRedisService.isExistsFocusUser(userInfo.getId(),focusUserId)){
                            continue;
                        }
                        userInfoRedisService.addFocusUser(userInfo.getId(),focusUserId);
                    }
                }

                //粉丝
                fansUserIdList = userInfoService.getFanUserIdList(userInfo.getId());
                if(fansUserIdList!=null&&fansUserIdList.size()>0){
                    for (Long fanUserId : fansUserIdList) {
                        if(userInfoRedisService.isExistsFansUser(userInfo.getId(),fanUserId)){
                            continue;
                        }
                        userInfoRedisService.addFansUser(fanUserId,userInfo.getId());
                    }
                }

                //黑名单
                blackUserIdList = userInfoService.blackUserIdList(userInfo.getId());
                if(blackUserIdList != null && blackUserIdList.size()>0){
                    for (Long blackUserId : blackUserIdList) {
                        if(userInfoRedisService.isExistBlackUser(userInfo.getId(),blackUserId)){
                            continue;
                        }
                        userInfoRedisService.addBlackUser(userInfo.getId(),blackUserId);
                    }
                }

                //我的游记统计对象初始化
                List<Travel> travelList = travelService.getTravelListByUserId(userInfo.getId());
                String userTravelSetKey = RedisKeys.USER_TRAVEL_SET.join(userInfo.getId().toString());
                String viewnumKey = RedisKeys.TRAVEL_NOW_VIEW.join(userInfo.getId().toString());
                travelStatisVOService.setTravelViewNumNow(viewnumKey);
                for (Travel travel : travelList) {
                    String travelStatisVOKey = RedisKeys.TRAVEL_STATIS_VO.join(travel.getId().toString());
                    travelStatisVOService.addTravelStatisVOSet(userTravelSetKey,travelStatisVOKey);
                }

            }
        }


        //游记统计初始化
        List<Travel> travellist = travelService.list();
        for (Travel travel : travellist) {
            //如果vo已经存在了，跳过，防止再次初始化，覆盖数据
            if(travelStatisVOService.isTravelVOExist(travel.getId())){
                continue; //跳过就好
            }
            TravelStatisVO vo = new TravelStatisVO();
            BeanUtils.copyProperties(travel,vo);
            vo.setDestId(travel.getDest().getId());
            vo.setDestName(travel.getDest().getName());
            vo.setTravelId(travel.getId());
            travelStatisVOService.setTravelStatisVO(vo);
        }

        System.out.println("---------------------------------------- vo数据初始化 end --------------------------------------------------");

        System.out.println("---------------------------------------- zset数据初始化 end --------------------------------------------------");

        //热门初始化
        for (StrategyDetail detail : list) {
            String voId = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            //判断voId是否zai在指定的zset中
            if(strategyStatisVOService.isVoidExsitZset(voId,zsetKey)){
                continue;
            }
            int score = detail.getReplynum()+detail.getViewnum();
            strategyStatisVOService.addScoreHot(detail.getId(),score);
        }
        //国内外操作
        for (StrategyDetail detail : list) {

            String voId = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = null;
            if(detail.isIsabroad()){
                //国外
                zsetKey = RedisKeys.STRATEGY_ABROAD_ZSET.getPrefix();
            }else{
                //国内
                zsetKey = RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();
            }

            //判断voId是否zai在指定的zset中
            if(strategyStatisVOService.isVoidExsitZset(voId,zsetKey)){
                continue;
            }
            //点赞和收藏
            int score = detail.getThumbsupnum()+detail.getFavornum();
            strategyStatisVOService.addScoreAbroadAndChina(detail.getId(),score);
        }

        System.out.println("---------------------------------------- zset数据初始化 end --------------------------------------------------");


    }
}
